﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;
using TerrainGenerator.Geometry;
using JigLibX.Utils;

namespace TerrainGenerator.Physics
{
    class HeightmapObject : PhysicsObject
    {
        public HeightmapObject(Game game, float[,] heightmap)
            : base(Vector3.Zero, Vector3.One)
        {
            int width = heightmap.GetLength(0);
            int height = heightmap.GetLength(1);

            float offset_x = width / 2.0f;
            float offset_z = height / 2.0f;
            Array2D array = new Array2D(width, height, 0.0f);
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    array.SetAt(x, y, heightmap[x, y]);
                }
            }

            body = new Body();
            skin = new CollisionSkin(null);
            body.MoveTo(new Vector3(offset_x, 0, offset_z), Matrix.Identity);

            skin.AddPrimitive(new Heightmap(array, offset_x, offset_z, 1, 1), new MaterialProperties(0.5f, 1.0f, 1.0f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(skin);
        }


    }
}
